package com.ruoyi.yljf.service.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.yljf.domain.PaperTaxon;
import com.ruoyi.yljf.domain.Question;
import com.ruoyi.yljf.domain.vo.adminVO.TestPaperAdminVO;
import com.ruoyi.yljf.mapper.PaperTaxonMapper;
import com.ruoyi.yljf.mapper.QuestionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.TestPaperMapper;
import com.ruoyi.yljf.domain.TestPaper;
import com.ruoyi.yljf.service.ITestPaperService;

import javax.annotation.Resource;

/**
 * 考试-题目关联表Service业务层处理
 * 
 * @author metaee
 * @date 2025-04-25
 */
@Service
public class TestPaperServiceImpl extends ServiceImpl<TestPaperMapper, TestPaper> implements ITestPaperService {
    @Resource
    private TestPaperMapper testPaperMapper;
    @Resource
    private QuestionMapper questionMapper;
    @Resource
    private PaperTaxonMapper paperTaxonMapper;
    
    /**
     * 查询考试-题目关联表列表
     * 
     * @param testPaper 考试-题目关联表
     * @return 考试-题目关联表
     */
    @Override
    public List<TestPaper> selectTestPaperList(TestPaper testPaper) {
    	LambdaQueryWrapper<TestPaper> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotNull(testPaper.getPaperTaxonId())) {
			queryWrapper.eq(TestPaper::getPaperTaxonId, testPaper.getPaperTaxonId());
		}
        if (StringUtils.isNotNull(testPaper.getCourseQuestionId())) {
			queryWrapper.eq(TestPaper::getCourseQuestionId, testPaper.getCourseQuestionId());
		}
        if (StringUtils.isNotEmpty(testPaper.getSort())) {
			queryWrapper.eq(TestPaper::getSort, testPaper.getSort());
		}
        return testPaperMapper.selectList(queryWrapper);
    }

    @Override
    public List<TestPaperAdminVO> haldleTestPaper(List<TestPaper> list) {
        Set<Long> quesIds = list.stream()
                .map(TestPaper::getCourseQuestionId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        Set<Long> paperIds = list.stream()
                .map(TestPaper::getPaperTaxonId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        List<Question> questions = questionMapper.selectBatchIds(quesIds);
        List<PaperTaxon> paperTaxons = paperTaxonMapper.selectBatchIds(quesIds);

        // 3. 将 Question 转换为 Map<ID, Question> 以便快速查找
        Map<Long, Question> questionMap = questions.stream()
                .collect(Collectors.toMap(Question::getId,  Function.identity()));
        // 3. 将 paperTaxons 转换为 Map<ID, paperTaxon> 以便快速查找
        Map<Long, PaperTaxon> paperMap = paperTaxons.stream()
                .collect(Collectors.toMap(PaperTaxon::getTaxonId,  Function.identity()));

        // 4. 遍历 list 并关联赋值
        List<TestPaperAdminVO> result = list.stream()
                .map(testPaper -> {
                    TestPaperAdminVO vo = new TestPaperAdminVO();
                    // 复制原有属性（假设有 BeanUtils 或手动 set）
                    BeanUtils.copyProperties(testPaper,  vo);

                    // 通过 courseQuestionId 匹配并赋值 Question
                    if (testPaper.getCourseQuestionId()  != null) {
                        vo.setQuestion(questionMap.get(testPaper.getCourseQuestionId()));
                    }
                    if (testPaper.getPaperTaxonId()  != null) {
                        vo.setPaperTaxon(paperMap.get(testPaper.getPaperTaxonId()));
                    }

                    return vo;
                })
                .collect(Collectors.toList());


        return result;
    }
}
